package util;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import model.assessment.Question;
import model.assessment.QuestionDB;
import model.assessment.Template;
import model.assessment.TemplateDB;
import model.hospital.Building;
import model.hospital.BuildingDB;
import model.hospital.Patient;
import model.hospital.PatientDB;
import model.user.Staff;
import model.user.StaffDB;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.TreeSet;

public class DataInit {
    public static void initialize(){        //初始化数据到内存
        questionInit();
        templateInit();
        buildingInit();
        patientInit();
        staffInit();
    }

    private static void questionInit(){
        ObjectMapper om = new ObjectMapper();
        File f = new File("Question_DB.json");
        try {
            LinkedList<Question> questions = om.readValue(f, new TypeReference<LinkedList<Question>>(){});
            if(questions!=null)questions.removeIf(Question::isDeleted);
            QuestionDB.initialize(questions!=null? new ArrayList<>(questions): null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static void templateInit(){
        ObjectMapper om = new ObjectMapper();
        File f = new File("Template_DB.json");
        try {
            LinkedList<Template> templates = om.readValue(f, new TypeReference<LinkedList<Template>>(){});
            if(templates!=null)templates.removeIf(Template::isDeleted);
            TemplateDB.initialize(templates!=null? new ArrayList<>(templates): null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static void buildingInit(){
        ObjectMapper om = new ObjectMapper();
        File f = new File("Building_DB.json");
        try {
            TreeSet<Building> buildings = om.readValue(f, new TypeReference<TreeSet<Building>>(){});
            BuildingDB.initialize(buildings);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    private static void patientInit(){
        ObjectMapper om = new ObjectMapper();
        File f = new File("Patient_DB.json");
        try {
            LinkedList<Patient> patients = om.readValue(f, new TypeReference<LinkedList<Patient>>(){});
            if(patients!=null)patients.removeIf(Patient::isDeleted);
            PatientDB.initialize(patients!=null? new ArrayList<>(patients):null);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    private static void staffInit(){
        ObjectMapper om = new ObjectMapper();
        File f = new File("Staff_DB.json");
        try {
            LinkedList<Staff> staffs = om.readValue(f, new TypeReference<LinkedList<Staff>>(){});
            if(staffs!=null)staffs.removeIf(Staff::isDeleted);
            StaffDB.initialize(staffs!=null? new ArrayList<>(staffs): null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}